// SECRETID 和 SECRETKEY请登录 https://console.cloud.tencent.com/cam/capi 进行查看和管理
var COS = require('cos-nodejs-sdk-v5');

// 导出对象，供其它模块调用
module.exports = thisFunction;

async function thisFunction(req, request) {
    let postData = req.body
    let returnData = Common.clone(Constant.DEFAULT_SUCCESS);

    let checkResult = Common.checkUrl(postData, ['imageUrl'], request);
    if (checkResult == false) return;

    if (postData.imageUrl.indexOf("ea567360-ef6d-11eb-96f0-f5e1ee6b1ebf.png") != -1) { // 默认商品图
        returnData.data = "默认商品图"
        return request.json(returnData)
    }

    let splitWords1 = postData.imageUrl.split("https://artppt-1306121076.cos.ap-nanjing.myqcloud.com/")
    let key = splitWords1[1]
    if (key) {
        if (key && key.indexOf("/sf") != -1) key == key.split("/sf")[0]
        if (key && key.indexOf("?imageMogr2/format/WebP") != -1) key == key.split("?imageMogr2/format/WebP")[0]

        var cos = new COS({
            SecretId: 'AKIDuc5GAnAG5duuHQJwHL8fusZz80bvykFT',
            SecretKey: 'zh8IAm7BxSgdJhpgFXZJmzvd5HDrEK9g'
        });

        cos.deleteObject({
            Bucket: 'artppt-1306121076', /* 必须 */
            Region: 'ap-nanjing',    /* 必须 */
            Key: key    /* 必须 */
        }, function (err, data) {
            console.log(err, data)
            if (err) {
                returnData.code = 0
                returnData.data = err
            } else {
                returnData.data = data
            }
            return request.json(returnData)
        });
    } else {
        return request.json(returnData)
    }

}